package com.quarantine.web.admin;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.quarantine.model.trends;
import com.quarantine.service.TrendsManager;
import com.quarantine.util.DateUtils;
import com.quarantine.web.CrudActionSupport;


@Namespace("/admin")
@Results({
	@Result(name ="save", location = "trends-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"}),
	@Result(name ="delete", location = "trends-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"})
})
public class TrendsAdminAction extends CrudActionSupport<trends> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7579617689188959519L;
	private trends entity;
	private TrendsManager trends;
	private String id;
	private List<trends> trendsList;
	private String ids;
	private String time;
	/**
	 * 分頁用
	 */
	private int currentpage;
	private int pageNo;
	public static final int pageSize = 20;
	private int prePage;
	private long totalPages;
	private int nextPage;
	private long allsize;

	/**
	 * 列出公告列表 
	 */
		@Override
		public String list() throws Exception {
			int pageSize = 9;
			Page<trends> page = new Page<trends>();
			page.setAutoCount(true);
			page.setPageNo(currentpage);
			page.setPageSize(pageSize);
		    trends.getList(page);
			setTrendsList(page.getResult());
			pageNo = page.getPageNo();
			prePage = page.getPrePage();
			nextPage = page.getNextPage();
			totalPages = page.getTotalPages();
            if(totalPages==0){
				
				pageNo=0;
			}
			allsize = page.getTotalCount();
			return "list";
		}

		@Override
		public String input() throws Exception {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String save() throws Exception { 
			if(id!=null&&id!="")
			 {
				 ;
			 }
			else{
				 
					entity.setClickrate(0);
				 
			}
			
			
			 
			 trends.save(entity);
			return "save";
		}
/**
 * 删除单个
 */
		@Override
		public String delete() throws Exception {
			trends.delete(id);
			return "delete";
		}

		@Override
		protected void prepareModel() throws Exception {
			if (StringUtils.isEmpty(id)) {
				entity = new trends();
			} else {
				entity = trends.getById(id);
			}
			
		}
		public String show() {
			entity = trends.getById(id);
			return "addnews";
		}	
		public String show1() {

		    setTime(DateUtils.now());
			return "addnews";
		}

		/**
		 * 按id批量删除
		 */
		public String deleteByIds() {
	          for (String id : ids.split(";")) 
	          {
		        entity = trends.getById(id);
		       
		        trends.delete(id);
		       }
	         return "delete";
		}
	public trends getEntity() {
		return entity;
	}

	public void setEntity(trends entity) {
		this.entity = entity;
	}

	public TrendsManager getTrends() {
		return trends;
	}
	@Autowired
	public void setTrends(TrendsManager trends) {
		this.trends = trends;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<trends> getTrendsList() {
		return trendsList;
	}

	public void setTrendsList(List<trends> trendsList) {
		this.trendsList = trendsList;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public long getAllsize() {
		return allsize;
	}

	public void setAllsize(long allsize) {
		this.allsize = allsize;
	}

	@Override
	public com.quarantine.model.trends getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

}
